//
//  UIView+Draw.m
//  TouchTV
//
//  Created by rhc on 16/8/31.
//  Copyright © 2016年 AceWei. All rights reserved.
//

#import "UIView+Draw.h"
#import "UIView+Toast.h"

@implementation UIView (Draw)

- (void)addPortraitGradientLayer:(UIColor *)topColor bottomColor:(UIColor*)bottomColor{
    if(topColor == nil || bottomColor == nil) return;
    CAGradientLayer *gradientLayer =  [CAGradientLayer layer];
    gradientLayer.frame = self.bounds;
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(0, 1);
    gradientLayer.locations = @[@(0.3),@(0.8)];//渐变点
    [gradientLayer setColors:@[(id)topColor.CGColor,(id)bottomColor.CGColor]];
    
    [self removeSublayersFromSuperlayer];
    [self.layer addSublayer:gradientLayer];
}

- (void)removeSublayersFromSuperlayer{
    NSMutableArray* list = [NSMutableArray arrayWithArray:self.layer.sublayers];
    for (CAShapeLayer *layer in list)
    {
        [layer removeFromSuperlayer];
    }
}
- (UIView *)drawBounderWidth:(CGFloat)width  Color:(UIColor *)color {
    [self.layer setBorderWidth:width];
    [self.layer setBorderColor:color.CGColor];
    return self;
}

- (UIView *)drawBounderWidth:(CGFloat)width radius:(CGFloat)radius Color:(CGColorRef)color {
    [self.layer setBorderWidth:width];
    if(color){
        [self.layer setBorderColor:color];
    }
    if (self.layer.cornerRadius != radius) {
        [self.layer setCornerRadius:radius];
        self.layer.masksToBounds = YES;
    }
    return self;
}

+ (CGFloat)commonFaceIdSafeBottom{
    CGFloat bottom = 0;
    if (@available(iOS 11.0, *)){
        bottom = [[[UIApplication sharedApplication] delegate] window].safeAreaInsets.bottom;
    }
    return bottom;
}

+ (CGFloat)commonFaceIdSafeTop{
    CGFloat top = 0;
    if (@available(iOS 11.0, *)){
        top = [[[UIApplication sharedApplication] delegate] window].safeAreaInsets.top;
    }
    return top;
}
@end


@implementation UIImage (Color)

+ (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    [color setFill];
    UIRectFill(rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}


- (UIImage *)imageWithColor:(UIColor *)color
{
    UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0, self.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    CGContextClipToMask(context, rect, self.CGImage);
    [color setFill];
    
    CGContextFillRect(context, rect);
    UIImage*newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}




@end
